package com.zhiyu.controller;

import com.github.pagehelper.PageInfo;
import com.zhiyu.common.ApiReturn;
import com.zhiyu.pojo.FlowerWood;
import com.zhiyu.service.FlowerWoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@RequestMapping("flowerWood")
@Controller
@CrossOrigin
public class FlowerWoodController {

    @Autowired
    private FlowerWoodService flowerWoodService;

    //花材管理 获取花材
    @RequestMapping("flowerWoodList")
    @ResponseBody
    public ApiReturn getFlowerWoodList(FlowerWood flowerWood){
        PageInfo<FlowerWood> page = flowerWoodService.findPage(flowerWood);
        if (page.getList() != null){
            return ApiReturn.success("返回花材数据",page);
        }
        return ApiReturn.error("无数据");
    }

    //删除花材
    @RequestMapping("deleteFlowerWood")
    @ResponseBody
    public ApiReturn deleteFlowerWood(FlowerWood flowerWood){
        int count = flowerWoodService.delete(flowerWood);
        if (count<1){
            return ApiReturn.error("删除失败");
        }
        return ApiReturn.success("删除成功");
    }

    //修改花材
    @RequestMapping("updateFlowerWood")
    @ResponseBody
    public ApiReturn updateFlowerWood(FlowerWood flowerWood){
        int count = flowerWoodService.update(flowerWood);
        if (count<1){
            return ApiReturn.error("修改失败");
        }
        return ApiReturn.success("修改成功");
    }

    //添加花材
    @RequestMapping("addFlowerWood")
    @ResponseBody
    public ApiReturn addFlowerWood(FlowerWood flowerWood){
        flowerWood.setGmtCreate(new Date());
        int count = flowerWoodService.insert(flowerWood);
        if (count<1){
            return ApiReturn.error("添加失败");
        }
        return ApiReturn.success("添加成功");
    }

    //根据花材名称搜索
    @RequestMapping("getFlowerWoodByName")
    @ResponseBody
    public ApiReturn getFlowerWoodName(FlowerWood flowerWood){
        PageInfo<FlowerWood> page = flowerWoodService.getFlowerWoodByName(flowerWood);
        if (page.getList() != null){
            return ApiReturn.success("返回花材数据",page);
        }
        return ApiReturn.error("无数据");
    }
}
